package com.proj.hot.activity.webview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebBackForwardList;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;
//import com.tencent.smtt.sdk.WebView;
//import com.tencent.smtt.sdk.WebViewClient;


import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.activity.LoginActivity;
import com.proj.hot.utils.DebugUtil;
import com.proj.hot.utils.TimeService;

import java.io.File;

public class WebMainActivity extends Activity {

    private WebView webView;

    //http://14.215.173.21:8090/2.html
    //private String url0 = "http://wxgd.scpayback.atianqi.com/boss/wxgd/news_web.html?id=news_1705188&parentcode=x_wx_yj_zy_bd_news&appcode=wxyj&city=&appcode=wxyj";
    private String url0 = "http://192.168.51.92:8010/boss/FTV/pages.html";
     //private String url0 = "http://192.168.51.91:9330/ftweb/pages.html";//正式测试地址
    //private String url0 = "www.baidu.com";
    //private String url0 = "http://183.3.131.82:8210/ftweb/pages.html";

    private static final String APP_CACAHE_DIRNAME = "/webviewCache";

    ImageView webface;





    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);



            if (msg.what == 1) {

                webface.setVisibility(View.GONE);

            }
        }
    };





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        url0=getIntent().getStringExtra("weburl");

        setContentView(R.layout.activity_webmain);
        webface=(ImageView) findViewById(R.id.webface);
        webView = (WebView) findViewById(R.id.wv1);

        webView.clearCache(true);
        clearWebViewCache();
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        //webView.setBackgroundColor(Color.parseColor("#000000"));
        webView.setWebViewClient(   new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                webView.loadUrl(url);

                return true;
            }
        });


        //支持javascript
        webView.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        webView.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        webView.getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放
        webView.getSettings().setUseWideViewPort(true);
        //自适应屏幕
//        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setLoadWithOverviewMode(true);


        // 开启 DOM storage API 功能
        webView.getSettings().setDomStorageEnabled(true);
        //开启 database storage API 功能
//        webView.getSettings().setDatabaseEnabled(true);
        String cacheDirPath = getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME;
        //设置数据库缓存路径
        webView.getSettings().setGeolocationDatabasePath(cacheDirPath);
//        webView.getSettings().setDatabasePath(cacheDirPath);
        //设置  Application Caches 缓存目录
        webView.getSettings().setAppCachePath(cacheDirPath);
        //开启 Application Caches 功能
        webView.getSettings().setAppCacheEnabled(true);
//        webView.getSettings().setMediaPlaybackRequiresUserGesture(false);



        webView.loadUrl(url0);

        handler.sendEmptyMessageDelayed(1,1500);

    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if(webView.canGoBack()) {
                //获取webView的浏览记录
                WebBackForwardList mWebBackForwardList = webView.copyBackForwardList();
                //这里的判断是为了让页面在有上一个页面的情况下，跳转到上一个html页面，而不是退出当前activity
                if (mWebBackForwardList.getCurrentIndex() > 0) {
                    String historyUrl = mWebBackForwardList.getItemAtIndex(mWebBackForwardList.getCurrentIndex() - 1).getUrl();

                        webView.goBack();



                    return true;
                }else{

                }





            }
        }

        if(event.getKeyCode()==KeyEvent.KEYCODE_MENU){
            startActivity(new Intent(Settings.ACTION_SETTINGS)); //直接进入手机中设置界面
        }

        return super.onKeyDown(keyCode, event);
    }





    /**
     * 清除WebView缓存
     */
    public void clearWebViewCache(){

        //清理Webview缓存数据库
        try {
            deleteDatabase("webview.db");
            deleteDatabase("webviewCache.db");
        } catch (Exception e) {
            e.printStackTrace();
        }

        //WebView 缓存文件
        File appCacheDir = new File(getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME);

        File webviewCacheDir = new File(getCacheDir().getAbsolutePath()+"/webviewCache");

        //删除webview 缓存目录
        if(webviewCacheDir.exists()){
            deleteFile(webviewCacheDir);
        }
        //删除webview 缓存 缓存目录
        if(appCacheDir.exists()){
            deleteFile(appCacheDir);
        }
    }




    /**
     * 递归删除 文件/文件夹
     *
     * @param file
     */
    public void deleteFile(File file) {


        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else if (file.isDirectory()) {
                File files[] = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    deleteFile(files[i]);
                }
            }
            file.delete();
        } else {

        }
    }

}
